<!--获取手机验证码-->
<template>
	<view class="getcode">
		<button :disabled="isDisabled" @click="getcode">{{butname}}</button>
	</view>
</template>

<script>
	export default {
		name: 'getCode',
		data() {
			return {
				isDisabled: false,
				butname: '获取验证码',
				code: '',
				time: 59,

			}
		},
		props: ["verification", "type"], //获取父组件传的值
		mounted() {},
		methods: {
			getcode: function() { //获取短信验证码
				let _this = this
				// 				let interval = setInterval(function() {
				// 					_this.butname = '(' + _this.time + '秒)'
				// 						--_this.time
				// 					if (_this.time < 0) {
				// 						_this.butname = '重新发送'
				// 						_this.time = 60
				// 						_this.isDisabled = false
				// 						clearInterval(interval)
				// 					}
				// 				}, 1000)
				if (/^(1[3,4,5,6,7,8,9][0-9])\d{8}$/.test(_this.verification)) {
					_this.isDisabled = true;
					_this.mobileCodeF();
					return false;
				}
				if (/^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/.test(_this.verification)) {
					_this.isDisabled = true;
					_this.emailCodef();
					return false;
				}

				uni.showToast({
					icon: 'none',
					title: '请输入正确手机号'
				});
			},
			//手机验证码
			mobileCodeF() {
				const _this = this;
				const dataT = {
					mobile: _this.verification,
					event: _this.type
				}
				this.$request.post("/api/sms/send", {
						data: dataT,
					})
					.then(res => {
						uni.showToast({
							icon: 'none',
							title: res.data.msg
						});
						if (res.data.code == 1) {
							let interval = setInterval(function() {
								_this.butname = '(' + _this.time + '秒)'
									--_this.time
								if (_this.time < 0) {
									_this.butname = '重新发送'
									_this.time = 60
									_this.isDisabled = false
									clearInterval(interval)
								}
							}, 1000)
						} else {
							_this.isDisabled = false
						}
					})
					.catch(error => {
						console.error('error:', error);
					});

			},
			//邮箱验证码
			emailCodef() {
				const _this = this;

				this.$request.post("/api/sms/send", {
						data: dataT,
					})
					.then(res => {
						uni.showToast({
							icon: 'none',
							title: res.data.msg
						});
						if (res.code == 1) {
							let interval = setInterval(function() {
								_this.butname = '(' + _this.time + '秒)'
									--_this.time
								if (_this.time < 0) {
									_this.butname = '重新发送'
									_this.time = 60
									_this.isDisabled = false
									clearInterval(interval)
								}
							}, 1000)
						} else {
							_this.isDisabled = false
						}
					})
					.catch(error => {
						console.error('error:', error);
					});


			}
		}
	}
</script>

<style>
	.getcode {
		width: 100%;
		height: 100%;
	}

	button {
		background: #1d7bff;
		color: #fff;
		outline: none;
		border: none;
		width: 100%;
		height: 100%;
		padding: 0;
		font-size: 12px;
	}
</style>
